package springBoot.akai.Akai.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import springBoot.akai.Akai.Mapper.TagMapper;

@RestController
public class TagController {
	@Autowired
	private TagMapper tagMapper;
	
	@RequestMapping(value = "/tag/add", method = RequestMethod.POST)
	String handleTagAdd(String tagName, int videoId) {
		Integer tagId = tagMapper.selectTagIdByName(tagName);

		if (tagId == null) {
			if (tagMapper.insertTag(tagName)) {
				tagId = tagMapper.selectLastInsertId();
			} else {
				return "false";
			}
		}
		
		
		if (tagMapper.createTagVideoLink(tagId, videoId)) {
			return "true";
		} else {
			return "false";
		}
	}
	
	@RequestMapping(value = "/tag/remove", method = RequestMethod.POST)
	String handleTagRemove(int tagId, int videoId) {
		if (tagMapper.deleteTagVideoLink(tagId, videoId) && tagMapper.deleteTagById(tagId)) {
			return "true";
		} else {
			return "false";
		}
	}
}
